package com.idopartx.phonelightning.application

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.multidex.MultiDex
import androidx.multidex.MultiDexApplication
import com.beef.keepalive.KeepAlive
import com.dotools.toutiaolibrary.TTManagerHolder
import com.dotools.umlibrary.UMPostUtils
import com.dotools.utils.PackageUtils
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.ido.news.splashlibrary.util.Constant
import com.ido.switchmodel.SMHolder
import com.ido.switchmodel.util.SMType
import com.idopartx.phonelightning.service.NotificationMonitorService
import com.idopartx.phonelightning.service.SmsService
import com.idopartx.phonelightning.service.TelListenerService
import com.idopartx.phonelightning.utils.AppUtils
import com.idopartx.phonelightning.utils.ProcessLifecycleObserver
import com.tencent.mmkv.MMKV

class CLApplication : MultiDexApplication() {

    companion object {
        var isFullVideoIng = false //此静态变量⽤在FullVideoActivity当中
    }

    private val lifecycleObserver by lazy {
        ProcessLifecycleObserver(this).apply {
            limitName = arrayOf(                                                //数组中列出的⻚⾯为不显示间隔开屏逻辑
                "SplashActivity",
                "FullVideoActivity",
                "TTFsEpVkActivity",
                "TTFsVkActivity",
                "TTDelegateActivity",
                "TTLPActivity",
                "TastefulThemeActivity",
                "Stub_Standard_Portrait_Activity",
                "WXEntryActivity"
            )
        }
    }

    private fun initSplashProcessLife() {
        ProcessLifecycleOwner.get().lifecycle.addObserver(lifecycleObserver)
        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {


            override fun onActivityCreated(
                activity: Activity, savedInstanceState:
                Bundle?
            ) {
                Log.e("onActivityCreated", activity.localClassName)
                lifecycleObserver.nowShowActivityName = activity.localClassName
            }

            override fun onActivityStarted(activity: Activity) {
            }

            override fun onActivityResumed(activity: Activity) {
                Log.e("onActivityResumed", activity.localClassName)
                lifecycleObserver.nowShowActivityName = activity.localClassName
            }

            override fun onActivityPaused(activity: Activity) {
            }

            override fun onActivityStopped(activity: Activity) {
            }

            override fun onActivitySaveInstanceState(
                activity: Activity, outState:
                Bundle
            ) {
            }

            override fun onActivityDestroyed(activity: Activity) {
            }

        })
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        KeepAlive.run(base)
        MMKV.initialize(base)
        initSplashProcessLife()


        if (!AppUtils.isServiceRunning(base, TelListenerService::class.java.name)) {
            val telService = Intent(base, TelListenerService::class.java)
            startService(telService)
        }

        if (!AppUtils.isServiceRunning(base, NotificationMonitorService::class.java.name)) {
            val notifyService = Intent(base, NotificationMonitorService::class.java)
            startService(notifyService)
        }

        if (!AppUtils.isServiceRunning(
                base,
                SmsService::class.java.name
            ) && XXPermissions.isGranted(
                base,
                Permission.READ_SMS,
                Permission.RECEIVE_MMS,
                Permission.READ_CONTACTS
            )
        ) {
            val smsService = Intent(
                base,
                SmsService::class.java
            )
            startService(smsService)
        }
    }

    override fun onCreate() {
        super.onCreate()

        if (!MMKV.defaultMMKV().decodeBool("isFirstJoin", true)) {
            init()
        }


        SMHolder.getInstance().getHotSplash().init(
            applicationContext,
            applicationContext.packageName,
            PackageUtils.getVersionCode(applicationContext).toString(),
            PackageUtils.getUmengChannel(applicationContext),
            SMType.HOT_SPLASH
        )
    }


    //初始化
    fun init() {
        UMPostUtils.init(applicationContext)
        UMPostUtils.setDebugLog(false) //头条初始化
        TTManagerHolder.doInit(this, Constant.JRTT_APPID, false) //广点通初始化
    }
}